var gulp = require("gulp")
var $ = require('gulp-load-plugins')()
var open = require('open')

var app = {
	srcPath:'src/',
	devPath:'build/',
	prdPath:'dist/'
};

gulp.task('lib',function(){
	return gulp.src('bower_components/**/*.js')
	.pipe(gulp.dest(app.devPath + 'vendor'))
	.pipe(gulp.dest(app.prdPath + 'vendor'))
});
gulp.task('html',function () {
	return gulp.src(app.srcPath + '**/*.html')
	.pipe(gulp.dest(app.devPath))
	.pipe(gulp.dest(app.prdPath))
})
gulp.task('json',function(){
	return gulp.src(app.srcPath+'data/**/*.json')
	.pipe(gulp.dest(app.devPath + 'data'))
	.pipe(gulp.dest(app.prdPath + 'data'))
});
gulp.task('less',function(){
	return gulp.src(app.srcPath + 'style/main.less')
	.pipe($.less())
	.pipe(gulp.dest(app.devPath + 'css'))
	.pipe($.cssmin())
	.pipe(gulp.dest(app.prdPath + 'css'))
})
gulp.task('js',function(){
	return gulp.src(app.srcPath + 'script/**/*.js')
	.pipe($.concat('index.js'))
	.pipe(gulp.dest(app.devPath + 'js'))
	.pipe($.uglify())
	.pipe(gulp.dest(app.prdPath + 'js'))
})
gulp.task('image', function() {
  return gulp.src(app.srcPath + 'image/**/*')
  .pipe($.plumber())
  .pipe(gulp.dest(app.devPath + 'image'))
  .pipe($.imagemin({
                 options: {
                        optimizationLevel: 5,
                        progressive: true,
                      },

                    files: [{
                        expand: true,
                        cwd: 'assets/images',  // that folder should exist with all images
                        src: ['**/*.{png,jpg,gif}'],
                        dest: 'public/images'   // where is going your min files
                    }]

            }))
  .pipe(gulp.dest(app.prdPath + 'image'))
  .pipe($.connect.reload());
});

// gulp.task('build', ['image', 'js', 'less', 'lib', 'html', 'json']);

// gulp.task('build',['image','js','less','lib','html','json'],function() {
// });

gulp.task('clean',function(){
	return gulp.src([app.devPath,app.prdPath])
	.pipe($.clean());
})

gulp.task('serve',function () {
	return $.connect.server({
		root:[app.devPath],
		livereload:true,
		port:8080
	});
	open('http://localhost:2345')
})
